

//
//  YPBaseCollectionView.m
//  project
//
//  Created by 姚敦鹏 on 2018/10/13.
//  Copyright © 2018年 rocHome. All rights reserved.
//

#import "YPBaseCollectionView.h"

#define EmptyTitleColor HEXColor(0x666666)
#define EmptyTipColor   HEXColor(0x999999)

@interface YPBaseCollectionView ()

@property (nonatomic,assign) BOOL isPullUp;
@property (nonatomic,assign) BOOL isPullDown;

@property (nonatomic,copy) NSArray *normalImages;
@property (nonatomic,copy) NSArray *refreshImages;

@end

@implementation YPBaseCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout isPullUp:(BOOL)isPullUp isPullDown:(BOOL)isPullDown {
    
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        [self setEmptyDelegate];
        [self setIsPullDown:isPullDown withIsPullUp:isPullUp];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame isPullUp:(BOOL)isPullUp isPullDown:(BOOL)isPullDown {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setEmptyDelegate];
        [self setIsPullDown:isPullDown withIsPullUp:isPullUp];
    }
    return self;
}

#pragma mark -- empty
- (void)setEmptyDelegate {
    
    self.emptyDataSetSource   = self;
    self.emptyDataSetDelegate = self;
}

-(void)setIsPullDown:(BOOL)isPullDown
        withIsPullUp:(BOOL)isPullUp
{
    self.isPullUp   = isPullUp;
    self.isPullDown = isPullDown;
    [self YPPullRefreshInit];
}

- (void)YPPullRefreshInit {
    
    if (self.isPullDown) {
        MJRefreshGifHeader *gifHeader = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            
            // 重置没有更多
            self.mj_footer ? [self.mj_footer resetNoMoreData] : nil;
            self.pullDownBlock ? self.pullDownBlock(self) : [self pullRefreshEnd];
        }];
        self.mj_header = gifHeader;
        [gifHeader setImages:self.normalImages forState:MJRefreshStateIdle];
        [gifHeader setImages:self.refreshImages forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [gifHeader setImages:self.refreshImages forState:MJRefreshStateRefreshing];
        self.mj_header.automaticallyChangeAlpha = YES;
    } else {
        self.mj_header = nil;
    }
    
    if (self.isPullUp) {
        MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            self.pullUpBlock ? self.pullUpBlock(self) : [self pullRefreshEnd];
        }];
        self.mj_footer = footer;
        [footer setImages:self.normalImages forState:MJRefreshStateIdle];
        [footer setImages:self.refreshImages forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [footer setImages:self.refreshImages forState:MJRefreshStateRefreshing];
        
        self.mj_footer.automaticallyChangeAlpha = YES;
    } else {
        self.mj_footer = nil;
    }
}

- (void)pullRefreshEnd {
    
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

- (void)pullRefreshNotMore {
    
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshingWithNoMoreData];
}

- (void)setPullDownBlock:(PullBlock)pullDownBlock
         withPullUpBlock:(PullBlock)pullUpBlock
{
    self.pullDownBlock = pullDownBlock;
    self.pullUpBlock   = pullUpBlock;
}

#pragma mark -- DZNEmptyDataSetSource
// 空白页显示图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    UIImage *emptyImage = IMAGENANED(@"emptyNothing");
    if ([YPReachability shareInstall].isHasNet) {
        switch (self.emptyStatus) {
            case YPPageEmptyStatusError:
                emptyImage = IMAGENANED(@"emptyError");
                break;
            case YPPageEmptyStatusNoData:
                emptyImage = IMAGENANED(@"emptyNothing");
                break;
            default:
                emptyImage = IMAGENANED(@"emptyNoNet");
                break;
        }
    } else {
        emptyImage = IMAGENANED(@"emptyNoNet");
    }
    return emptyImage;
}

// 空白页显示标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *title = @"暂无数据";
    if ([YPReachability shareInstall].isHasNet) {
        switch (self.emptyStatus) {
            case YPPageEmptyStatusError:
                title = @"页面开小差了";
                break;
            case YPPageEmptyStatusNoData:
                title = @"暂无数据";
                break;
            default:
                title = @"网络走丢了";
                break;
        }
    } else {
        title = @"网络走丢了";
    }
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f],
                                 NSForegroundColorAttributeName:EmptyTitleColor
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

// 空白页显示详细描述
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *text = @"点击重新加载";
    if ([YPReachability shareInstall].isHasNet) {
        switch (self.emptyStatus) {
            case YPPageEmptyStatusError:
                text = @"网络繁忙，请稍候再试";
                break;
            case YPPageEmptyStatusNoData:
                text = @"点击重新加载";
                break;
            default:
                text = @"检查您的网络或点击重新加载";
                break;
        }
    } else {
        text = @"检查您的网络或点击重新加载";
    }
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName:EmptyTipColor,
                                 NSParagraphStyleAttributeName:paragraph
                                 };
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

#pragma mark -- DZNEmpty Delegate
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    
    return self.emptyStatus != YPPageEmptyStatusLoading && self.emptyStatus != YPPageEmptyStatusHidden;
}

-(CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -kNavigationBarH;
}

-(BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

-(void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    self.emptyTapBlock ? self.emptyTapBlock(self) : nil;
}

@end
